package web.servlet;

import com.alibaba.fastjson.JSON;
import domain.Product;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import service.CatelogService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class CategoryShowJsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryid=req.getParameter("categoryid");
        CatelogService service=new CatelogService();
        List<Product> productList=service.getProductListByCategory(categoryid);
        String result= JSON.toJSONString(productList);

        System.out.println("success");
        resp.setContentType("text/json");
        PrintWriter out=resp.getWriter();
        out.println(result);
    }

    //    protected void doGET(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String categoryId = request.getParameter("categoryId");
//        System.out.println(categoryId+"             ddddddddd");
//        CatelogService catalogService = new CatelogService();
//        List<Product> productList = catalogService.getProductListByCategory(categoryId);
//        String resp = "Product ID      Name\n";
//        int i = 0;
//        while(i < productList.size()){
//            Product product = productList.get(i);
//            resp += product.getProductId() + "      " + product.getName() + "\n";
//            i++;
//        }
//
//        response.setContentType("text/xml");
//        PrintWriter out = response.getWriter();
//        out.write(resp);
//
//    }
//
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        doPost(request, response);
//    }
}
